#coding:utf-8
from django.db import models
from django.db.models.signals import pre_delete, post_save
from django.dispatch import receiver
import json
from metlib.misc.misc import str2obj
from weblib.django_kits import *
from weblib.django.user import *
from weblib.django.setting import *
import uuid


# Create your models here.
class LittleLog(models.Model):
    class Meta:
        app_label = 'littler'
    log_id = models.AutoField(primary_key=True)  # logid
    filename = models.CharField(
        max_length=255, default='',blank=True)  # 实际的dataset类型的名字
    datafile_dt = models.DateTimeField()  # 更新时间
    update_dt = models.DateTimeField(default=local_now)  # 更新时间

    def __unicode__(self):
        return self.name

    # 数据集模板类型
    @property
    def mold_class(self):
        return str2obj(self.classname)


class LittlePoint(models.Model):
    class Meta:
        unique_together = (("latitude", "longitude"),)
        app_label = 'littler'

    point_id = models.CharField(max_length=255, unique=True)  # logid
    latitude = models.FloatField()
    longitude = models.FloatField()
    station_id = models.CharField(max_length=255, default='', null=True)
    station_name = models.CharField(max_length=255, default='', null=True)
    platform = models.CharField(max_length=255, default='', null=True)
    data_count = models.IntegerField(default=0)
    last_data_time = models.DateTimeField(default=None)
    frist_data_time = models.DateTimeField(default=None)
    def __unicode__(self):
        return self.name

    # 数据集模板类型
    @property
    def mold_class(self):
        return str2obj(self.classname)

class LittleData(models.Model):
    class Meta:
        app_label = 'littler'

    data_id = models.CharField(max_length=255, default='', primary_key=True)  # logid
    data_type = models.CharField(max_length=255, default='', null=True)  # 数据类型压缩格式有别(unique)
    source = models.TextField(default='', null=True)
    elevation = models.FloatField(default=None, null=True)
    little_point = models.ForeignKey(
        'LittlePoint', to_field='point_id', blank=True)  # littledata外键
    num_valid_fields = models.IntegerField(default=None, null=True)
    num_error = models.IntegerField(default=None, null=True)
    num_warning = models.IntegerField(default=None, null=True)
    seq_num = models.IntegerField(default=None, null=True)
    num_dups = models.IntegerField(default=None, null=True)
    is_sound = models.BooleanField()
    bogus = models.BooleanField()
    discard = models.BooleanField()
    sut = models.IntegerField(default=None, null=True)
    julian = models.IntegerField(default=None, null=True)
    date_char = models.DateTimeField()
    slp = models.FloatField(default=None, null=True)
    slp_qc = models.IntegerField(default=None, null=True)
    ref_pres = models.FloatField(default=None, null=True)
    ref_pres_qc = models.IntegerField(default=None, null=True)
    ground_t = models.FloatField(default=None, null=True)
    ground_t_qc = models.IntegerField(default=None, null=True)
    psfc = models.FloatField(default=None, null=True)
    psfc_qc = models.IntegerField(default=None, null=True)
    precip = models.FloatField(default=None, null=True)
    precip_qc = models.IntegerField(default=None, null=True)
    t_max = models.FloatField(default=None, null=True)
    t_max_qc = models.IntegerField(default=None, null=True)
    t_min = models.FloatField(default=None, null=True)
    t_min_qc = models.IntegerField(default=None, null=True)
    t_min_night = models.FloatField(default=None, null=True)
    t_min_night_qc = models.IntegerField(default=None, null=True)
    cloud_cvr = models.FloatField(default=None, null=True)
    cloud_cvr_qc = models.IntegerField(default=None, null=True)
    update_dt = models.DateTimeField(default=local_now)  # 更新时间
    level_info = models.TextField()
    def __unicode__(self):
        return self.name

    # 数据集模板类型
    @property
    def mold_class(self):
        return str2obj(self.classname)


# class LittleDataLevel(models.Model):
#     class Meta:
#         pass
#
#     level_id = models.AutoField(primary_key=True)  # 级别自增序列
#     little_data = models.ForeignKey(
#         'LittleData', to_field='data_id', blank=True)  # littledata外键
#     level = models.IntegerField(null=True)  # 级别
#     pres = models.FloatField(default=None, null=True)
#     height = models.FloatField(default=None, null=True)  # 高度
#     temp = models.FloatField(default=None, null=True)  #
#     dew = models.FloatField(default=None, null=True)  #
#     wspd = models.FloatField(default=None, null=True)  #
#     dir = models.FloatField(default=None, null=True)  #
